package com.ita.controller;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ita.service.IUserService;
import com.ita.vo.User;

@Controller(value="userController")
@Scope
@RequestMapping("/user")
@SessionAttributes({"u"})
public class UserController {

//	private IUserService service=new UserServiceImpl();
	private IUserService service;
	
	
	public IUserService getService() {
		return service;
	}

	@Resource
	public void setService(IUserService service) {
		this.service = service;
	}

	@ModelAttribute("u")
	public User setUserName(String username,String password){
		System.out.println("controller hello:"+username+","+password);
		return new User();
	}
	
	@RequestMapping(value="/login",method=(RequestMethod.POST))
	public String login(@ModelAttribute("u") User u1){
		User user=service.login(u1);
		System.out.println("login controller:"+user);
		if(user!=null){
			return "success";
		}
		else{
			return "fail";
		}
	}
	
	@RequestMapping(value="/register",method=RequestMethod.POST)
	public String register(String username,String password){
		User user=new User(username, password);
		int line=service.register(user);
		if(line>0){
			return "login";
		}
		else{
			return "register";
		}
	}
	
	
	
	@RequestMapping("/getBook")
	public String getBook(){
		return "success";
	}
}
